﻿<classes:MSHPhoneApplicationPage xmlns:my="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"  
    xmlns:bewise="clr-namespace:Bewise.Phone;assembly=Bewise.Phone"    
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"  
    x:Class="WP7Client.UserInterface.Pages.OverviewPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.WP7"
    xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:classes="clr-namespace:WP7Client.Classes"
    xmlns:rb="clr-namespace:WP7Client.RelativeBinding"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True" Loaded="MSHPhoneApplicationPage_Loaded" Name="theOverviewPage">

    <classes:MSHPhoneApplicationPage.Resources>
        <classes:BooleanToInvertedBooleanConverter x:Key="BooleanToInvertedBooleanConverter"/>
        <classes:ObjectToVisibilityConverter x:Key="ObjectToVisibilityConverter"/>
        <classes:DateTimeFormatConverter x:Key="DateTimeFormatConverter"/>
        <classes:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
        <classes:FriendImageConverter x:Key="FriendImageConverter" />
        <classes:PrivacyEnumConverter x:Key="PrivacyEnumConverter" />
        <classes:ByteArrayToImageSource x:Key="ByteArrayToImageSource" />
        <classes:GenderEnumConverter x:Key="GenderEnumConverter"/>
        <classes:ObservableCollectionCountToVisibility x:Key="ObservableCollectionCountToVisibility"/>
        <classes:ObjectWithPrivacyToVisibilityConverter x:Key="ObjectWithPrivacyToVisibilityConverter"/>
        <classes:ObjectWithPrivacyToProfileCommandByPeopleNearbyConverter x:Key="ObjectWithPrivacyToProfileCommandByPeopleNearbyConverter"/>
        <classes:ObjectWithPrivacyToProfileCommandByInvitationsConverter x:Key="ObjectWithPrivacyToProfileCommandByInvitationsConverter"/>
        <classes:UserLocationConverter x:Key="UserLocationConverter" />
        <classes:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
        <classes:MapVisibilityEnumConverter x:Key="MapVisibilityEnumConverter" />
        <classes:NullToBooleanConverter x:Key="NullToBooleanConverter" />
    </classes:MSHPhoneApplicationPage.Resources>
    
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="0" Name="gridStatus">
            <StackPanel Orientation="Horizontal" >
                <Image Margin="10,0,0,0" Name="ImageConnected" Visibility="{Binding IsConnected, Mode=TwoWay, BindsDirectlyToSource=True, Converter={StaticResource BoolToVisibilityConverter}}" Source="/WP7Client;component/Resources/Images/Connected.png" Width="40" HorizontalAlignment="Left"/>
                <Image Margin="10,0,0,0" Name="ImageDisconnected" Visibility="{Binding IsConnected, Mode=TwoWay, BindsDirectlyToSource=True, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=invert}" Source="/WP7Client;component/Resources/Images/Disconnected.png" Width="40" HorizontalAlignment="Left"/>
                <TextBox Text="{Binding Status, Mode=TwoWay}" Width="325"/>
                <Button cmd:ButtonBaseExtensions.Command="{Binding ApplyStatusCommand}">
                    <Image Source="/WP7Client;component/Resources/Images/Apply.png" Margin="-7,-7,-8,-8"/>
                </Button>
            </StackPanel>
        </Grid>
        <TextBlock Grid.Row="1" Name="TextBlockNewMessages" Text="New message(s)!" Foreground="Red" Margin="10,0,0,0" Visibility="{Binding GotNewUnreadMessages, Converter={StaticResource BoolToVisibilityConverter}, Mode=TwoWay, BindsDirectlyToSource=True}"/>
        <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="10,0,0,0">
            <Image Source="/WP7Client;component/Resources/Images/globe_icon.png" Height="28"/>
            <TextBlock Text="{Binding PeopleNearbyCount}" Margin="4,0,0,0"/>
            <TextBlock Text="{Binding CurrentLocation, Converter={StaticResource UserLocationConverter}}" Margin="6,0,0,0" Visibility="{Binding CurrentLocation, Converter={StaticResource NullToVisibilityConverter}}"/>
        </StackPanel>
            
        <controls:Pivot Name="thePivot" Title="MY SOCIAL HUB" Grid.Row="3">

            <controls:PivotItem x:Name="PivotItemFriends" Header="Friends" IsEnabled="{Binding Loading, Converter={StaticResource BooleanToInvertedBooleanConverter}}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <Button Grid.Row="0" Name="ButtonFriendsMap" Margin="-10,-40,0,0" BorderThickness="0" cmd:ButtonBaseExtensions.Command="{Binding ShowFriendsOnMapCommand}" HorizontalContentAlignment="Left" >
                        <StackPanel Orientation="Horizontal" Margin="0, -10, 0, -10">
                            <TextBlock Text="Show on map "/>
                            <Image Source="/WP7Client;component/Resources/Images/globe_icon.png" />
                        </StackPanel>
                    </Button>

                    <bewise:JumpList Grid.Row="1" Name="JumpListFriends" IsPumped="False" ItemsSource="{Binding Friends, Mode=OneWay}" HideRowHeaderBackgroundIfPicturePresent="True" ShowHeaders="True" ShowRowHeader="False">
                        <bewise:JumpList.ItemTemplate>
                            <DataTemplate>                                
                                <StackPanel>
                                    <toolkit:ContextMenuService.ContextMenu>
                                        <toolkit:ContextMenu Name="ContextMenuFriends" Background="Black" BorderBrush="White" BorderThickness="5">
                                            <toolkit:MenuItem Header="Chat" Foreground="White" FontSize="35" Command="{Binding ElementName=DataHolder, Path=Data.ChatCommand}" CommandParameter="{Binding Username}" Visibility="{Binding Blocked, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=invert}"/>
                                            <toolkit:MenuItem Header="Profile" Foreground="White" FontSize="35" Command="{Binding ElementName=DataHolder, Path=Data.ShowProfileCommand}" CommandParameter="{Binding Username}" Visibility="{Binding Blocked, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=invert}"/>
                                            <toolkit:MenuItem Header="Delete" Foreground="White" FontSize="35" Command="{Binding ElementName=DataHolder, Path=Data.RemoveFriendCommand}" CommandParameter="{Binding Username}"/>
                                            <toolkit:MenuItem Header="Block" Foreground="White" FontSize="35" Command="{Binding ElementName=DataHolder, Path=Data.BlockFriendCommand}" CommandParameter="{Binding Username}" Visibility="{Binding Blocked, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=invert}"/>
                                            <toolkit:MenuItem Header="Unblock" Foreground="White" FontSize="35" Command="{Binding ElementName=DataHolder, Path=Data.UnblockFriendCommand}" CommandParameter="{Binding Username}" Visibility="{Binding Blocked, Converter={StaticResource BoolToVisibilityConverter}}"/>
                                            <toolkit:MenuItem Header="Notifications" Foreground="White" FontSize="35" Command="{Binding ElementName=DataHolder, Path=Data.FriendNotificationCommand}" CommandParameter="{Binding Username}"/>
                                        </toolkit:ContextMenu>
                                    </toolkit:ContextMenuService.ContextMenu>

                                    <rb:DataHolder x:Name="DataHolder">
                                        <rb:BindingHelper.Binding>
                                            <rb:BindingProperties
                                                SourceProperty="DataContext"
                                                TargetProperty="Data"
                                                RelativeSourceAncestorType="PivotItem">
                                            </rb:BindingProperties>
                                        </rb:BindingHelper.Binding>
                                    </rb:DataHolder>

                                    <Button Margin="-90,-15,0,-15" BorderThickness="0" cmd:ButtonBaseExtensions.Command="{Binding ElementName=DataHolder, Path=Data.ShowProfileCommand}" cmd:ButtonBaseExtensions.CommandParameter="{Binding Username}" >
                                        <StackPanel Orientation="Horizontal">
                                            <Image Source="{Binding Blocked, Mode=OneWay, Converter={StaticResource FriendImageConverter}}" Height="100" Width="100"/>
                                            <StackPanel>
                                                <StackPanel Orientation="Horizontal" Margin="10,0,0,-5">
                                                    <TextBlock Text="{Binding FirstName}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}}" HorizontalAlignment="Left" FontSize="28" Margin="0,0,5,0"/>
                                                    <TextBlock Text="{Binding SurName}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}}" HorizontalAlignment="Left" FontSize="28" />
                                                </StackPanel>
                                                <TextBlock Text="{Binding Username}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}, ConverterParameter=invert}" FontSize="28" Margin="10,0,0,-5"/>
                                                <TextBlock Text="{Binding Status}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}" FontSize="17"/>
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Text="Last seen at:" TextWrapping="Wrap" HorizontalAlignment="Left" Style="{StaticResource PhoneTextSubtleStyle}" FontSize="17"/>
                                                    <TextBlock Text="{Binding LastSeenTimestamp, Converter={StaticResource DateTimeFormatConverter}}" TextWrapping="Wrap" HorizontalAlignment="Left" Style="{StaticResource PhoneTextSubtleStyle}" FontSize="17"/>
                                                </StackPanel>
                                                <StackPanel>
                                                    <TextBlock Text="{Binding Location.DisplayablePosition}" HorizontalAlignment="Left" Style="{StaticResource PhoneTextSubtleStyle}" FontSize="17"/>
                                                </StackPanel>
                                            </StackPanel>
                                        </StackPanel>
                                    </Button>
                                </StackPanel>
                            </DataTemplate>
                        </bewise:JumpList.ItemTemplate>
                    </bewise:JumpList>
                    <ProgressBar VerticalAlignment="Top" IsIndeterminate="{Binding Loading}"/>
                </Grid>
            </controls:PivotItem>

            <controls:PivotItem Header="Conversations" Name="PivotItemConverstations" IsEnabled="{Binding Loading, Converter={StaticResource BooleanToInvertedBooleanConverter}}">
                <Grid>
                    <ListBox Name="ListBoxConverstations" ItemsSource="{Binding Conversations, Mode=OneWay}" >
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Margin="0,0,0,-10">
                                    <toolkit:ContextMenuService.ContextMenu>
                                        <toolkit:ContextMenu Name="ContextMenuConversations" Background="Black" BorderBrush="White" BorderThickness="5">
                                            <toolkit:MenuItem Header="Open" Foreground="White" FontSize="35" Command="{Binding DataContext.ShowChatCommand, ElementName=PivotItemConverstations}" CommandParameter="{Binding ChatWithFriend}"/>
                                            <toolkit:MenuItem Header="Delete" Foreground="White" FontSize="35" Command="{Binding DataContext.DeleteChatCommand, ElementName=PivotItemConverstations}" CommandParameter="{Binding ChatWithFriend}"/>
                                        </toolkit:ContextMenu>
                                    </toolkit:ContextMenuService.ContextMenu>
                                    <Button cmd:ButtonBaseExtensions.Command="{Binding DataContext.ShowChatCommand, ElementName=PivotItemConverstations}" cmd:ButtonBaseExtensions.CommandParameter="{Binding ChatWithFriend}">
                                        <StackPanel Orientation="Horizontal">
                                            <Image Source="/WP7Client;component/Resources/Images/chat_icon.png"/>
                                            <StackPanel>
                                                <TextBlock Text="{Binding ChatWithFriend}" Margin="10,0,0,0"/>
                                                <TextBlock Text="{Binding LastMessageReceivedTimestamp}" Style="{StaticResource PhoneTextSubtleStyle}" />
                                                <TextBlock Text="{Binding LastMessage}" Style="{StaticResource PhoneTextSubtleStyle}" FontStyle="Italic"/>
                                            </StackPanel>
                                        </StackPanel>
                                    </Button>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                    <ProgressBar VerticalAlignment="Top" IsIndeterminate="{Binding Loading}"/>
                </Grid>                
            </controls:PivotItem>

            <controls:PivotItem Header="People nearby" Name="PivotItemPeopleNearby" IsEnabled="{Binding Loading, Converter={StaticResource BooleanToInvertedBooleanConverter}}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <Button Grid.Row="0" Name="ButtonPeopleNearbyMap" Margin="-10,-40,0,0" BorderThickness="0" cmd:ButtonBaseExtensions.Command="{Binding ShowMapCommand}" HorizontalContentAlignment="Left">
                        <StackPanel Orientation="Horizontal" Margin="0, -10, 0, -10">
                            <TextBlock Text="Show on map "/>
                            <Image Source="/WP7Client;component/Resources/Images/globe_icon.png" />
                        </StackPanel>
                    </Button>
                    <ListBox Grid.Row="1">
                        <Grid Width="450">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <Border Grid.Row="0" Background="{StaticResource PhoneAccentBrush}" Visibility="{Binding FriendsNearby, Converter={StaticResource ObservableCollectionCountToVisibility}}">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Friends" Margin="10,4,0,4" FontSize="40" Style="{StaticResource TextBlockStyle}"/>
                                </StackPanel>
                            </Border>
                            <ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled" Grid.Row="1" Name="ListBoxFriendsNearby" ItemsSource="{Binding FriendsNearby, Mode=OneWay}" Visibility="{Binding FriendsNearby, Converter={StaticResource ObservableCollectionCountToVisibility}}">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel>
                                            <toolkit:ContextMenuService.ContextMenu>
                                                <toolkit:ContextMenu Name="ContextMenuPeopleNearby" Background="Black" BorderBrush="White" BorderThickness="5">
                                                    <toolkit:MenuItem Header="Send invitation" Foreground="White" FontSize="35" Command="{Binding DataContext.AddFriendCommand, ElementName=PivotItemPeopleNearby}" CommandParameter="{Binding Username}" Visibility="{Binding isFriend, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=invert}"/>
                                                    <toolkit:MenuItem Header="Chat" Foreground="White" FontSize="35" Command="{Binding DataContext.ChatCommand, ElementName=PivotItemPeopleNearby}" CommandParameter="{Binding Username}"/>
                                                    <toolkit:MenuItem Header="View profile" Foreground="White" FontSize="35" Command="{Binding DataContext.ShowProfileCommand, ElementName=PivotItemPeopleNearby}" CommandParameter="{Binding Username}" Visibility="{Binding Converter={StaticResource ObjectWithPrivacyToVisibilityConverter}}"/>
                                                    <toolkit:MenuItem Header="Notifications" Foreground="White" FontSize="35" Command="{Binding DataContext.FriendNotificationCommand, ElementName=PivotItemPeopleNearby}" CommandParameter="{Binding Username}" Visibility="{Binding isFriend, Converter={StaticResource BoolToVisibilityConverter}}"/>
                                                </toolkit:ContextMenu>
                                            </toolkit:ContextMenuService.ContextMenu>

                                            <Button cmd:ButtonBaseExtensions.Command="{Binding Converter={StaticResource ObjectWithPrivacyToProfileCommandByPeopleNearbyConverter}}" cmd:ButtonBaseExtensions.CommandParameter="{Binding Username}" Margin="-5,0,0,-15" BorderThickness="0">
                                                <StackPanel Orientation="Horizontal">
                                                    <Image Source="/WP7Client;component/Resources/Images/avatar.png" Height="100" Width="100"/>
                                                    <StackPanel>
                                                        <StackPanel Orientation="Horizontal" Margin="10,0,0,-10">
                                                            <TextBlock Text="{Binding FirstName}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}}" HorizontalAlignment="Left" FontSize="28" Margin="0,0,5,0"/>
                                                            <TextBlock Text="{Binding SurName}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}}" HorizontalAlignment="Left" FontSize="28" />
                                                        </StackPanel>
                                                        <TextBlock Text="{Binding Username}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}, ConverterParameter=invert}"  Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                        <TextBlock Text="{Binding Status}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}"  />
                                                        <StackPanel Orientation="Horizontal">
                                                            <TextBlock Text="Last seen at:" TextWrapping="Wrap" HorizontalAlignment="Left"  Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                            <TextBlock Text="{Binding LastSeenTimestamp, Converter={StaticResource DateTimeFormatConverter}}" TextWrapping="Wrap" HorizontalAlignment="Left"  Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                        </StackPanel>
                                                        <StackPanel>
                                                            <TextBlock Text="{Binding DisplayablePosition}" HorizontalAlignment="Left" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                        </StackPanel>
                                                    </StackPanel>
                                                </StackPanel>
                                            </Button>
                                        </StackPanel>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>

                            <Border Grid.Row="2" Margin="0,10,0,0" Background="{StaticResource PhoneAccentBrush}" Visibility="{Binding FriendsNearby, Converter={StaticResource ObservableCollectionCountToVisibility}}">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Other" Margin="10,4,0,4" FontSize="40" Style="{StaticResource TextBlockStyle}"/>
                                </StackPanel>
                            </Border>
                            <ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled" Grid.Row="3" Name="ListBoxPeopleNearby" ItemsSource="{Binding PeopleNearby, Mode=OneWay}" >
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel>
                                            <toolkit:ContextMenuService.ContextMenu>
                                                <toolkit:ContextMenu Name="ContextMenuPeopleNearby" Background="Black" BorderBrush="White" BorderThickness="5">
                                                    <toolkit:MenuItem Header="Send invitation" Foreground="White" FontSize="35" Command="{Binding DataContext.AddFriendCommand, ElementName=PivotItemPeopleNearby}" CommandParameter="{Binding Username}"/>
                                                    <toolkit:MenuItem Header="Chat" Foreground="White" FontSize="35" Command="{Binding DataContext.ChatCommand, ElementName=PivotItemPeopleNearby}" CommandParameter="{Binding Username}"/>
                                                    <toolkit:MenuItem Header="View profile" Foreground="White" FontSize="35" Command="{Binding DataContext.ShowProfileCommand, ElementName=PivotItemPeopleNearby}" CommandParameter="{Binding Username}" Visibility="{Binding Converter={StaticResource ObjectWithPrivacyToVisibilityConverter}}"/>
                                                </toolkit:ContextMenu>
                                            </toolkit:ContextMenuService.ContextMenu>

                                            <Button cmd:ButtonBaseExtensions.Command="{Binding Converter={StaticResource ObjectWithPrivacyToProfileCommandByPeopleNearbyConverter}}" cmd:ButtonBaseExtensions.CommandParameter="{Binding Username}" Margin="-5,0,0,-15" BorderThickness="0">
                                                <StackPanel Orientation="Horizontal">
                                                    <Image Source="/WP7Client;component/Resources/Images/avatar.png" Height="100" Width="100"/>
                                                    <StackPanel>
                                                        <StackPanel Orientation="Horizontal" Margin="10,0,0,-10">
                                                            <TextBlock Text="{Binding FirstName}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}}" HorizontalAlignment="Left" FontSize="28" Margin="0,0,5,0"/>
                                                            <TextBlock Text="{Binding SurName}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}}" HorizontalAlignment="Left" FontSize="28" />
                                                        </StackPanel>
                                                        <TextBlock Text="{Binding Username}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}, ConverterParameter=invert}"  Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                        <TextBlock Text="{Binding Status}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}"  />
                                                        <StackPanel Orientation="Horizontal">
                                                            <TextBlock Text="Last seen at:" TextWrapping="Wrap" HorizontalAlignment="Left"  Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                            <TextBlock Text="{Binding LastSeenTimestamp, Converter={StaticResource DateTimeFormatConverter}}" TextWrapping="Wrap" HorizontalAlignment="Left"  Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                        </StackPanel>
                                                        <StackPanel>
                                                            <TextBlock Text="{Binding DisplayablePosition}" HorizontalAlignment="Left" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                        </StackPanel>
                                                    </StackPanel>
                                                </StackPanel>
                                            </Button>
                                        </StackPanel>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>

                            <ProgressBar VerticalAlignment="Top" IsIndeterminate="{Binding Loading}"/>
                        </Grid>
                    </ListBox>
                </Grid>                
            </controls:PivotItem>

            <controls:PivotItem Header="Search" Name="PivotItemSearch" IsEnabled="{Binding Loading, Converter={StaticResource BooleanToInvertedBooleanConverter}}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="10"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>  

                    <StackPanel Grid.Row="1" Orientation="Horizontal">
                        <TextBlock Text="Username: " VerticalAlignment="Center"/>
                        <TextBox Text="{Binding Filter.Username, Mode=TwoWay}" Width="350" />
                    </StackPanel>

                    <StackPanel Grid.Row="2" Orientation="Horizontal">
                        <TextBlock Text="Firstname: " VerticalAlignment="Center"/>
                        <TextBox Text="{Binding Filter.Firstname, Mode=TwoWay}" Width="350"/>
                    </StackPanel>

                    <StackPanel Grid.Row="3" Orientation="Horizontal">
                        <TextBlock Text="Surname: " VerticalAlignment="Center"/>
                        <TextBox Text="{Binding Filter.Surname, Mode=TwoWay}" Width="360" />
                    </StackPanel>

                    <StackPanel Grid.Row="4" Orientation="Horizontal">
                        <TextBlock Text="City: " VerticalAlignment="Center"/>
                        <TextBox Text="{Binding Filter.City, Mode=TwoWay}" Width="260" />
                        <TextBlock Text="Age: " VerticalAlignment="Center"/>
                        <TextBox Text="{Binding Filter.Age, Mode=TwoWay}" Width="100" />
                    </StackPanel>
                    
                    <StackPanel Grid.Row="5" Orientation="Horizontal">
                        <TextBlock Text="Phone number:" VerticalAlignment="Center"/>
                        <TextBox Text="{Binding Filter.MobilePhoneNumber, Mode=TwoWay}" Width="310" InputScope="TelephoneNumber"/>
                    </StackPanel>

                    <Button Grid.Row="6" cmd:ButtonBaseExtensions.Command="{Binding DataContext.SearchCommand, ElementName=PivotItemSearch}" Content="Search" HorizontalAlignment="Left"/>

                </Grid>
            </controls:PivotItem>


            <controls:PivotItem Header="Suggestions" Name="PivotItemSuggestions"  IsEnabled="{Binding Loading, Converter={StaticResource BooleanToInvertedBooleanConverter}}">
                <Grid>
                    <ListBox Grid.Row="1">
                        <Grid Width="450">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <!--<Border Grid.Row="0" Visibility="{Binding Suggestions, Converter={StaticResource ObservableCollectionCountToVisibility}}">
                                <StackPanel Orientation="Horizontal">
                                </StackPanel>
                            </Border>-->
                            <ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled" Grid.Row="1" Name="ListBoxSuggestions" ItemsSource="{Binding Suggestions, Mode=OneWay}" Visibility="{Binding Suggestions, Converter={StaticResource ObservableCollectionCountToVisibility}}">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel>
                                            <toolkit:ContextMenuService.ContextMenu>
                                                <toolkit:ContextMenu Name="ContextMenuSuggestions" Background="Black" BorderBrush="White" BorderThickness="5">
                                                    <toolkit:MenuItem Header="Send invitation" Foreground="White" FontSize="35" Command="{Binding DataContext.AddFriendCommand, ElementName=PivotItemSuggestions}" CommandParameter="{Binding Username}"/>
                                                    <toolkit:MenuItem Header="View profile" Foreground="White" FontSize="35" Command="{Binding DataContext.ShowProfileCommand, ElementName=PivotItemSuggestions}" CommandParameter="{Binding Username}" Visibility="{Binding Converter={StaticResource ObjectWithPrivacyToVisibilityConverter}}"/>
                                                </toolkit:ContextMenu>
                                            </toolkit:ContextMenuService.ContextMenu>

                                            <Button cmd:ButtonBaseExtensions.Command="{Binding Converter={StaticResource ObjectWithPrivacyToProfileCommandBySuggestionsConverter}}" cmd:ButtonBaseExtensions.CommandParameter="{Binding Username}" Margin="-5,0,0,-15" BorderThickness="0">
                                                <StackPanel Orientation="Horizontal">
                                                    <Image Source="/WP7Client;component/Resources/Images/avatar.png" Height="100" Width="100"/>
                                                    <StackPanel>
                                                        <StackPanel Orientation="Horizontal" Margin="10,0,0,-10">
                                                            <TextBlock Text="{Binding FirstName}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}}" HorizontalAlignment="Left" FontSize="28" Margin="0,0,5,0"/>
                                                            <TextBlock Text="{Binding SurName}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}}" HorizontalAlignment="Left" FontSize="28" />
                                                        </StackPanel>
                                                        <TextBlock Text="{Binding Username}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}, ConverterParameter=invert}"  Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                        <TextBlock Text="{Binding Status}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}"  />
                                                        <StackPanel>
                                                            <TextBlock Text="{Binding DisplayablePosition}" HorizontalAlignment="Left" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                        </StackPanel>
                                                    </StackPanel>
                                                </StackPanel>
                                            </Button>
                                        </StackPanel>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                            <ProgressBar VerticalAlignment="Top" IsIndeterminate="{Binding Loading}"/>
                        </Grid>
                    </ListBox>
                </Grid>
            </controls:PivotItem>
            

            <controls:PivotItem Header="Invitations" Name="PivotItemInvitations" IsEnabled="{Binding Loading, Converter={StaticResource BooleanToInvertedBooleanConverter}}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="80"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <Button Grid.Row="0" Content="Add friend by phone contact" cmd:ButtonBaseExtensions.Command="{Binding AddFriendByPhoneContactCommand}"/>
                    
                    <ListBox Grid.Row="1" Name="ListBoxInvitations" ItemsSource="{Binding Invitations, Mode=OneWay}" >
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <toolkit:ContextMenuService.ContextMenu>
                                        <toolkit:ContextMenu Name="ContextMenuInvitations" Background="Black" BorderBrush="White" BorderThickness="5">
                                            <toolkit:MenuItem Header="View profile" Foreground="White" FontSize="35" Command="{Binding DataContext.ShowProfileCommand, ElementName=PivotItemInvitations}" CommandParameter="{Binding Username}" Visibility="{Binding Converter={StaticResource ObjectWithPrivacyToVisibilityConverter}}"/>
                                            <toolkit:MenuItem Header="Accept" Foreground="White" FontSize="35" Command="{Binding DataContext.AcceptInvitationCommand, ElementName=PivotItemInvitations}" CommandParameter="{Binding Username}"/>
                                            <toolkit:MenuItem Header="Deny" Foreground="White" FontSize="35" Command="{Binding DataContext.DeclineInvitationCommand, ElementName=PivotItemInvitations}" CommandParameter="{Binding Username}"/>
                                        </toolkit:ContextMenu>
                                    </toolkit:ContextMenuService.ContextMenu>

                                    <Button cmd:ButtonBaseExtensions.Command="{Binding Converter={StaticResource ObjectWithPrivacyToProfileCommandByInvitationsConverter}}" cmd:ButtonBaseExtensions.CommandParameter="{Binding Username}" Margin="-5,0,0,-15" BorderThickness="0">
                                        <StackPanel Orientation="Horizontal">
                                            <Image Source="/WP7Client;component/Resources/Images/avatar.png" Height="100" Width="100"/>
                                            <StackPanel>
                                                <StackPanel Orientation="Horizontal" Margin="10,0,0,-10">
                                                    <TextBlock Text="{Binding FirstName}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}}" HorizontalAlignment="Left" FontSize="28" Margin="0,0,5,0"/>
                                                    <TextBlock Text="{Binding SurName}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}}" HorizontalAlignment="Left" FontSize="28" />
                                                </StackPanel>
                                                <TextBlock Text="{Binding Username}" TextWrapping="Wrap" Visibility="{Binding FirstName, Converter={StaticResource ObjectToVisibilityConverter}, ConverterParameter=invert}"  Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                <TextBlock Text="{Binding Status}" TextWrapping="Wrap" Style="{StaticResource PhoneTextSubtleStyle}"  />
                                                <StackPanel Orientation="Horizontal">
                                                    <TextBlock Text="Last seen at:" TextWrapping="Wrap" HorizontalAlignment="Left"  Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                    <TextBlock Text="{Binding LastSeenTimestamp, Converter={StaticResource DateTimeFormatConverter}}" TextWrapping="Wrap" HorizontalAlignment="Left"  Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                </StackPanel>
                                                <StackPanel>
                                                    <TextBlock Text="{Binding DisplayablePosition}" HorizontalAlignment="Left" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                                </StackPanel>
                                            </StackPanel>
                                        </StackPanel>
                                    </Button>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                    <ProgressBar VerticalAlignment="Top" IsIndeterminate="{Binding Loading}"/>
                </Grid>
            </controls:PivotItem>

            <controls:PivotItem Header="Profile" Name="PivotItemMyProfile" IsEnabled="{Binding Loading, Converter={StaticResource BooleanToInvertedBooleanConverter}}">
                <ListBox>
                    <Grid Width="460">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Border Grid.Row="0" Background="{StaticResource PhoneAccentBrush}">
                            <TextBlock Text="Profile" Margin="10,4,0,4" FontSize="40" Style="{StaticResource TextBlockStyle}"/>
                        </Border>
                        <StackPanel Grid.Row="1">
                            <TextBlock Style="{StaticResource TextBlockStyle}" Text="First name:"/>
                            <TextBox Text="{Binding UserProfile.FirstName, Mode=TwoWay}"/>
                        </StackPanel>
                        <StackPanel Grid.Row="2">
                            <TextBlock Style="{StaticResource TextBlockStyle}" Text="SurName:"/>
                            <TextBox Text="{Binding UserProfile.SurName, Mode=TwoWay}"/>
                        </StackPanel>
                        <StackPanel Grid.Row="3">
                            <TextBlock Style="{StaticResource TextBlockStyle}" Text="Mobile phone number:"/>
                            <StackPanel Orientation="Horizontal">
                                <TextBox Text="{Binding UserProfile.MobilePhoneNumber, Mode=TwoWay}" IsReadOnly="True" Width="255"/>
                                <Button Content="Edit" cmd:ButtonBaseExtensions.Command="{Binding EditMobileNumberCommand}" />
                                <Button Content="Verify" cmd:ButtonBaseExtensions.Command="{Binding VerifyMobileNumberCommand}"/>
                            </StackPanel>
                        </StackPanel>
                        <StackPanel Grid.Row="4">
                            <TextBlock Style="{StaticResource TextBlockStyle}" Text="Address:"/>
                            <TextBox Text="{Binding UserProfile.Address, Mode=TwoWay}"/>
                        </StackPanel>
                        <StackPanel Grid.Row="5">
                            <TextBlock Style="{StaticResource TextBlockStyle}" Text="Zipcode:"/>
                            <TextBox Text="{Binding UserProfile.Zipcode, Mode=TwoWay}"/>
                        </StackPanel>
                        <StackPanel Grid.Row="6">
                            <TextBlock Style="{StaticResource TextBlockStyle}" Text="City:"/>
                            <TextBox Text="{Binding UserProfile.City, Mode=TwoWay}"/>
                        </StackPanel>
                        <StackPanel Grid.Row="7">
                            <TextBlock Style="{StaticResource TextBlockStyle}" Text="Sex:"/>
                            <toolkit:ListPicker SelectedIndex="{Binding UserProfile.Sex, Mode=TwoWay,Converter={StaticResource GenderEnumConverter}}">
                                <toolkit:ListPickerItem Content="Male"/>
                                <toolkit:ListPickerItem Content="Female"/>
                                <toolkit:ListPickerItem Content="Unknown"/>
                            </toolkit:ListPicker>
                        </StackPanel>
                        <StackPanel Grid.Row="8">
                            <TextBlock Style="{StaticResource TextBlockStyle}" Text="Date of birth:"/>
                            <toolkit:DatePicker Value="{Binding UserProfile.DateOfBirth, Mode=TwoWay}"/>
                        </StackPanel>
                        <StackPanel Grid.Row="9">
                            <TextBlock Style="{StaticResource TextBlockStyle}" Text="Website:"/>
                            <TextBox Text="{Binding UserProfile.Website, Mode=TwoWay}"/>
                        </StackPanel>
                        <Button Grid.Row="10" Content="Save" Width="110" HorizontalAlignment="Left" cmd:ButtonBaseExtensions.Command="{Binding DataContext.SaveProfileCommand, ElementName=PivotItemMyProfile}"/>
                        <ProgressBar VerticalAlignment="Top" IsIndeterminate="{Binding Loading}"/>
                    </Grid>
                </ListBox>
            </controls:PivotItem>

            <controls:PivotItem Header="Social networks" Name="PivotItemSocialNetworks" IsEnabled="{Binding Loading, Converter={StaticResource BooleanToInvertedBooleanConverter}}">
                <Grid>
                    <ListBox Name="ListboxSocialnetworks" ItemsSource="{Binding Accounts, Mode=OneWay}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <toolkit:ContextMenuService.ContextMenu>
                                        <toolkit:ContextMenu Name="ContextMenuSocialNetworks" Background="Black" BorderBrush="White" BorderThickness="5">
                                            <toolkit:MenuItem Header="Add" Foreground="White" FontSize="35" Command="{Binding DataContext.AuthenticateCommand, ElementName=PivotItemSocialNetworks}" CommandParameter="{Binding Name}" Visibility="{Binding Authenticated, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=invert}"/>
                                            <toolkit:MenuItem Header="Remove" Foreground="White" FontSize="35" Command="{Binding DataContext.RemoveCommand, ElementName=PivotItemSocialNetworks}" CommandParameter="{Binding Name}" Visibility="{Binding Authenticated, Converter={StaticResource BoolToVisibilityConverter}}"/>
                                        </toolkit:ContextMenu>
                                    </toolkit:ContextMenuService.ContextMenu>

                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <StackPanel Orientation="Horizontal" Grid.Column="0">
                                        <Image Width="50" Height="50" Source="{Binding Logo, Mode=OneWay, Converter={StaticResource ByteArrayToImageSource}}" Margin="0,0,15,0"/>
                                        <Image Height="20" Width="20" Source="/WP7Client;component/Resources/Images/Icons/circle_green_small.png" Visibility="{Binding Authenticated, Converter={StaticResource BoolToVisibilityConverter}}" Margin="0,0,15,0"/>
                                        <Image Height="20" Width="20" Source="/WP7Client;component/Resources/Images/Icons/circle_red_small.png" Visibility="{Binding Authenticated, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=invert}" Margin="0,0,15,0"/>
                                        <TextBlock  Height="23" Text="Network: "/>
                                        <TextBlock  Height="23" Text="{Binding Name}" />
                                    </StackPanel>
                                </Grid>

                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                    <ProgressBar VerticalAlignment="Top" IsIndeterminate="{Binding Loading}"/>
                </Grid>
            </controls:PivotItem>

            <controls:PivotItem Header="Settings" Name="PivotItemSettings">
                <ListBox>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                    
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Row="0" Grid.Column="0" Text="GPS interval: " Margin="10,30,0,0"/>
                        <toolkit:ListPicker Grid.Row="0" Grid.Column="1" Name="ListPickers" SelectedIndex="{Binding GPSIntervalIndex, Mode=TwoWay}">
                            <toolkit:ListPicker.Items>
                                <toolkit:ListPickerItem Content="5 minutes"/>
                                <toolkit:ListPickerItem Content="10 minutes"/>
                                <toolkit:ListPickerItem Content="15 minutes"/>
                                <toolkit:ListPickerItem Content="30 minutes"/>
                            </toolkit:ListPicker.Items>
                        </toolkit:ListPicker>

                        <TextBlock Grid.Row="1" Grid.Column="0" Text="Nearby range: " Margin="10,30,0,0"/>
                        <toolkit:ListPicker Grid.Row="1" Grid.Column="1" Name="ListPickersNearby" SelectedIndex="{Binding PeopleNearbyRangeIndex, Mode=TwoWay}">
                            <toolkit:ListPicker.Items>
                                <toolkit:ListPickerItem Content="500 m"/>
                                <toolkit:ListPickerItem Content="1 km"/>
                                <toolkit:ListPickerItem Content="5 km"/>
                                <toolkit:ListPickerItem Content="10 km"/>
                            </toolkit:ListPicker.Items>
                        </toolkit:ListPicker>

                    
                        <TextBlock Grid.Row="2" Grid.Column="0" Text="Profile privacy: " Margin="10,30,0,0"/>
                        <toolkit:ListPicker Grid.Row="2" Grid.Column="1" Name="ListPickersProfilePrivacy" SelectedIndex="{Binding ProfilePrivacy, Mode=TwoWay, Converter={StaticResource PrivacyEnumConverter}}">
                            <toolkit:ListPicker.Items>
                                <toolkit:ListPickerItem Content="Friends only" />
                                <toolkit:ListPickerItem Content="Public" />
                                <toolkit:ListPickerItem Content="Private" />
                            </toolkit:ListPicker.Items>
                        </toolkit:ListPicker>

                        <TextBlock Grid.Row="3" Grid.Column="0" Text="Location privacy: " Margin="10,30,0,0"/>
                        <toolkit:ListPicker Grid.Row="3" Grid.Column="1" Name="ListPickersLocationPrivacy" SelectedIndex="{Binding LocationPrivacy, Mode=TwoWay, Converter={StaticResource PrivacyEnumConverter}}">
                            <toolkit:ListPicker.Items>
                                <toolkit:ListPickerItem Content="Friends only" />
                                <toolkit:ListPickerItem Content="Public" />
                                <toolkit:ListPickerItem Content="Private" />
                            </toolkit:ListPicker.Items>
                        </toolkit:ListPicker>

                        <TextBlock Grid.Row="4" Grid.Column="0" Text="Show on map: " Margin="10,30,0,0" />
                        <toolkit:ListPicker Grid.Row="4" Grid.Column="1" Name="ListPickerMapVisibility" Width="270" SelectedIndex="{Binding MapVisibility, Mode=TwoWay, Converter={StaticResource MapVisibilityEnumConverter}}">
                            <toolkit:ListPicker.Items>
                                <toolkit:ListPickerItem Content="Friends and strangers" />
                                <toolkit:ListPickerItem Content="Friends only" />
                                <toolkit:ListPickerItem Content="Strangers only" />
                            </toolkit:ListPicker.Items>
                        </toolkit:ListPicker>

                        <Button Grid.Row="5" Grid.Column="0" Content="Save" Width="120" HorizontalAlignment="Left" cmd:ButtonBaseExtensions.Command="{Binding SaveSettingsCommand}"/>
                        <Button Grid.Row="5" Grid.Column="1" HorizontalAlignment="Left" Width="200" Content="Logout" cmd:ButtonBaseExtensions.Command="{Binding DataContext.LogoutCommand, ElementName=PivotItemSettings}"/>
                        <Button Grid.Row="6" Grid.Column="1" HorizontalAlignment="Left" Width="200" Content="About" cmd:ButtonBaseExtensions.Command="{Binding DataContext.AboutCommand, ElementName=PivotItemSettings}"/>
                    
 
                        <ProgressBar VerticalAlignment="Top" IsIndeterminate="{Binding Loading}"/>
                    </Grid>
                </ListBox>
            </controls:PivotItem>

        </controls:Pivot>
    </Grid>

    <!--Sample code showing usage of ApplicationBar-->
    <!--<phone:MSHPhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton x:Name="appbar_button1" IconUri="/Images/appbar_button1.png" Text="Button 1"/>
            <shell:ApplicationBarIconButton x:Name="appbar_button2" IconUri="/Images/appbar_button2.png" Text="Button 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem x:Name="menuItem1" Text="MenuItem 1"/>
                <shell:ApplicationBarMenuItem x:Name="menuItem2" Text="MenuItem 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:MSHPhoneApplicationPage.ApplicationBar>-->
    
</classes:MSHPhoneApplicationPage>
